// shopPages/integral/integral.js.
var sliderWidth = 40;
var app = getApp()
var WxParse = require('../../wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    integral:0,
    // 选项卡
    tabs: ["积分收入", "积分支出"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    add:[],
    shift:[],
    newSignBtnState: false,  //按钮签到状态
    continuityDays7: false,  //连续7
    continuityDays3: false,  //连续3
    myToday: '',           //周几
    newSignNum: 0,      //签到天数
    newSignIntegral: 0,  //签到积分
    sign_integral: 0,
    //是否已签到 周一到周日都有这个是否已签到 isSigned
    isNewSignedArr: [],
    indicatorDots: false,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    circular: true,
  },
  //-------新签到---------
  signNewFn: function (e) {
    var that = this;

    //console.log(that.data.isNewSignedArr);

    //签到积分函数
    that.signAddFen();
  },

  //新签到积分 连续 天数-积分： 周三+3：周一，周二，周三（1+1+3=5）； 周日+7：周一到周日（1+1+3+1+1+1+7=15）
  signAddFen: function () {
    var that = this;
    var url2 = "/mall/mallSign_v4"
    var params2 = {
      uid: app.globalData.userInfo.id,
      timestamp: new Date().getTime(),
    };
    params2.sign = app.getSign(params2)
    wx.showLoading({
      title: '签到中',
    })
    app.HttpPostSend(url2, params2, function (res) {
      wx.hideLoading();
      if (res.code > 0) {
        const arr = [],
            newSignArr = [...arr, ...that.data.isNewSignedArr];
        console.log(newSignArr);
        console.log(that.data.myToday);
        newSignArr[that.data.myToday].isSigned = true;

        var num = that.data.newSignNum;

        //当前积分
        num++;
        var curFen = parseFloat(that.data.newSignIntegral) + parseFloat(that.data.sign_integral);
        var day3 = new Date();
        var s3 = day3.getFullYear() + "-" + (day3.getMonth()+1) + "-" + day3.getDate() + " " + day3.getHours() + ":" + day3.getMinutes();
        var add_integral_prcei = that.data.sign_integral;
        add_integral_prcei = parseFloat(add_integral_prcei).toFixed(2)
        var add_log = { desc: '签到得积分', 'add_time': s3, 'integral': add_integral_prcei};
        var addLog = that.data.add;
        addLog.splice(0, 0, add_log);
        that.setData({
          add: addLog,
          newSignBtnState: true,
          newSignNum: num,
          newSignIntegral: curFen.toFixed(2),
          isNewSignedArr: newSignArr
        });
        var pages = getCurrentPages();//页面指针数组 
        var prepage = pages[pages.length - 2];//上一页面指针 
        var str = 'integral'
        prepage.setData({
          [str]: curFen.toFixed(2),

        });
      } else {
        app.showTipMsg(res.msg);
      }
    });
  },
  // 模态框 显
  task_guize: function () {
    var task_guize = this.data.task_guize
    this.setData({
      task_guize: true
    })
  },
  // 模态框 隐
  task_no: function () {
    var task_guize = this.data.task_guize
    this.setData({
      task_guize: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // console.log(options);
    // that.setData({
    //   p_cat_id: options.cat_id
    // });
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    });

    var url2 = "/mall/mallUserIntegral"
    var params2 = {
      uid: app.globalData.userInfo.id,
    };

    wx.showLoading({
      title: '玩命加载中',
    })
    app.HttpPostSend(url2, params2, function (res) {
      wx.hideLoading();

      if (res.code >0) {
        that.setData({
          integral: res.data.user_integral,
          add: res.data.add_log,
          shift: res.data.shift_log,
          newSignIntegral: res.sign_log.user_integral,
          isNewSignedArr: res.sign_log.week,
          sign_integral: res.sign_integral,
          newSignBtnState: res.sign_log.today_status,
          newSignNum: res.sign_log.sign_num
        })

        var content = res.integral_rule;
        WxParse.wxParse('content', 'html', content, that, 5);
      }
    });
    var that = this,
        myDate = new Date(),
        myToday = myDate.getDay();  //周几   0 1 2 3 4 5 6
    if (myToday > 0) {
      that.setData({
        myToday: myToday - 1
      })
    } else {
      that.setData({
        myToday: 6
      })
    }

    // console.log(app.globalData.userInfo);
    // wx.getSystemInfo({
    //   success: function (res) {
    //     that.setData({
    //       sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
    //       sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
    //     });
    //   }
    // });
    // var url2 = "/mall/mallIndexIntegral"
    // var params2 = {
    //   uid: app.globalData.userInfo.id,
    //   city_code: app.globalData.currentCode
    // };

    // wx.showLoading({
    //   title: '玩命加载中',
    // })
    // app.HttpPostSend(url2, params2, function (res) {
    //   wx.hideLoading();
    //   if (res.msg !== "success") {
    //     app.showTipMsg(res.msg);
    //   } else {
    //     console.log('测试' + res.banner_list)
    //     // console.log('图片' + res.banner_list.data.img)
    //     that.setData({
    //       // imgUrls: res.banner_list,
    //       grids: res.cat_list,
    //       home: res.new_list.lists.data,
    //       jfShop: res.rec_integral_list.lists,
    //       newSignIntegral: res.sign_log.user_integral,
    //       isNewSignedArr: res.sign_log.week,
    //       sign_integral: res.sign_integral,
    //       newSignBtnState: res.sign_log.today_status,
    //       newSignNum: res.sign_log.sign_num
    //       // s_list: res.recommend_list.lists.data,
    //       // s_list1: res.season_list.lists.data
    //     })
    //   }
    // });
  },
  tabClick: function (e) {
    this.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})